package com.example.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.servicebase.exceptionhandler.CustomException;
import com.example.teacher.entity.EduChapter;
import com.example.teacher.entity.EduVideo;
import com.example.teacher.entity.chapter.ChapterVo;
import com.example.teacher.entity.chapter.VideoVo;
import com.example.teacher.mapper.EduChapterMapper;
import com.example.teacher.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teacher.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideo(String id) {
        //根据课程id查询课程章节
        QueryWrapper<EduChapter> wrapper =new QueryWrapper<>();
        wrapper.eq("course_id",id);
        wrapper.orderByAsc("sort");
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);

        //根据id查询小节
        QueryWrapper<EduVideo> wrapperV =new QueryWrapper<>();
        wrapperV.eq("course_id",id);
        wrapperV.orderByAsc("sort");
        List<EduVideo> Videolist = videoService.list(wrapperV);
        //封装章节对象
        List<ChapterVo> ChapterList =new ArrayList<>();
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo=new ChapterVo();
            //根据属性进行赋值
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //封装小节
            List<VideoVo> videoVoList=new ArrayList<>();
            for (EduVideo eduVideo : Videolist) {
                //小节中，章节id等于当前章节的就进行封装
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            ChapterList.add(chapterVo);
        }
        return ChapterList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String id) {
        //先去查询该章节是否有小节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",id);
        long count = videoService.count(wrapper);
        if (count>0){//有小节不进行删除
            throw new CustomException(20001,"请先删除小节");
        }else{
        //没有小节可以删除
            int result = baseMapper.deleteById(id);
            return result>0;//大于表示删除了数据 ，小于0表示删除失败，则返回false
        }
    }
    //根据课程id删除章节
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
